package com.quasar.hpatient.view.personal.register.registerFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.BloodTypeInfodb;
import com.quasar.hpatient.model.litePalmodel.DurationInfodb;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.onInterface.viewInterface.UiInterface;
import com.quasar.hpatient.presenter.AloneUserPresenter;
import com.quasar.hpatient.view.personal.EditUserActivity;
import com.quasar.hpatient.view.personal.register.RegisterEditActivity;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/11/27 0027
 * View:注册血型
 */
@EFragment(R.layout.register_blood_fragment_layout)
public class RegisterBloodFragment extends BaseFragment implements UiInterface<DataStateResult> {

    private AloneUserPresenter aloneUserPresenter;
    //读取血型信息
    final List<BloodTypeInfodb> bloodTypeInfoList = MainAppliaction.getInstance().getBloodTypeInfoList();
    //记录血型id
    private int When=0;
    //单选血型选择
    @ViewById(R.id.id_flowlayout)
    TagFlowLayout id_flowlayout;
    @FragmentArg
    PatientInfoData patient;


    @AfterViews
    void init() {

        initView();
        initData();
    }

    @Click(R.id.tv_next)
    public void tv_next() {

        if (When == 0) {
            msg("请选择血型");
            return;
        }

        BloodTypeInfodb bloodTypeInfodb=null;
        for(int j=0;j<bloodTypeInfoList.size();j++){
            if(bloodTypeInfoList.get(j).getPrimaryid()==When){
                bloodTypeInfodb=bloodTypeInfoList.get(j);
                break;
            }
        }

        if(bloodTypeInfodb!=null){
            if (bloodTypeInfodb.getName().indexOf("阴性") != -1) {
                new AlertDialog.Builder(getActivity()).setTitle("确定选择当前血型吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                aloneUserPresenter.AloneUserData(ConstantManger.USERBLOODID, When + "", ConstantManger.TABLEUSERINFO);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).show();
            }else{
                aloneUserPresenter.AloneUserData(ConstantManger.USERBLOODID, When + "", ConstantManger.TABLEUSERINFO);
            }
        }

    }

    @Click(R.id.tv_last)
    public void tv_last() {
        RegisterEditActivity.ViewPage(3);
    }

    @Click(R.id.tv_skip)
    public void tv_skip() {
        RegisterEditActivity.ViewPage(5);
    }

    boolean isClick=false;
    @Override
    public void initView() {
        //单选
        final LayoutInflater mInflater = LayoutInflater.from(getActivity());
        TagAdapter tagAdapter=new TagAdapter<BloodTypeInfodb>(bloodTypeInfoList) {
            @Override
            public View getView(FlowLayout parent, int position, BloodTypeInfodb s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        id_flowlayout, false);
                tv.setText(s.getName());
                return tv;
            }
        };

        int index=0;
        for(int i=0;i<bloodTypeInfoList.size();i++){
            if(patient.getBloodId()==bloodTypeInfoList.get(i).getPrimaryid()){
                index=i;
                break;
            }
        }


        if(!isClick){
            if(patient.getBloodId()!=0){
                tagAdapter.setSelectedList(index);
                When=patient.getBloodId();
            }
            isClick=true;
        }

        id_flowlayout.setAdapter(tagAdapter);

        id_flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                When = bloodTypeInfoList.get(position).getPrimaryid();
                return true;
            }
        });

    }

    @Override
    public void initData() {
        aloneUserPresenter = new AloneUserPresenter(this);
    }


    @Override
    public void onViewSuccess(DataStateResult data) {
        if (data.getMsgCode().equals("0x00000000")) {
            RegisterEditActivity.ViewPage(5);
        }
    }

    @Override
    public void onViewError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onViewRefreshUI() {

    }
}
